package fhp.hlhj.giantfold.activity.malls

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.design.widget.TabLayout
import com.kepler.jd.login.KeplerApiManager
import com.kepler.jd.sdk.bean.KeplerAttachParameter
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.interfaces.ITags
import fhp.hlhj.giantfold.javaBean.TagsBean
import fhp.hlhj.giantfold.presenter.TagsPresenter
import fhp.hlhj.giantfold.utils.MyUtils
import kotlinx.android.synthetic.main.activity_search_good_aty.*
import me.kaede.tagview.Tag

class SearchGoodAty : BaseActivity(), ITags {
    private lateinit var tagsPresenter:TagsPresenter
    private var type=1;
    override fun getContentId(): Int {
        return R.layout.activity_search_good_aty
    }


    override fun initView() {
        tagView.setLineMargin(20f)//dp
        tagView.setTagMargin(20f)
        tagView.setTextPaddingLeft(20f)
        tagView.setTextPaddingRight(20f)

        tagsPresenter= TagsPresenter(this)
        tagsPresenter.getTags()



    }

    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        btSearch.setOnClickListener {
            if (etSearch.text.length==0){
                MyUtils.toast("搜点什么？")
            }else{
                if (type==1){
                     val intent = Intent(this, SearchResultAty::class.java)
                intent.putExtra("so",etSearch.text.toString())
                startActivity(intent)
                }else{
                    val parameter = KeplerAttachParameter()
                    parameter.setCustomerInfo("hlhjjz")
                    KeplerApiManager.getWebViewService().openSearchWebViewPage(etSearch.text.toString(), parameter)
                }
            }
//            MyUtils.toast("等待完善中")
        }

        tabLayout.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
            override fun onTabReselected(p0: TabLayout.Tab) {
            }

            override fun onTabUnselected(p0: TabLayout.Tab) {
            }

            override fun onTabSelected(p0: TabLayout.Tab) {
                when(p0.position){
                    0->{
                        type=1
                    }
                    1->{
                        type=2
                    }

                }
            }
        })
        btChooseType.setOnClickListener {
            if (type==1){
                type=2
                btChooseType.setImageResource(R.mipmap.ss_jd)
            }else{
                type=1
                btChooseType.setImageResource(R.mipmap.ss_t)
            }
        }
    }

    override fun showLoading() {
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun getTags(tagsBean: TagsBean) {
        if (tagsBean.code==200){
            tagsBean.data.forEachIndexed{
                index, dataBean ->
                val tag = Tag(dataBean.name)
                tag.background=getDrawable(R.drawable.tag_back)
                tag.tagTextColor=Color.parseColor("#666666")
                tagView.addTag(tag)
            }

            tagView.setOnTagClickListener { position, tag ->
                if (type==1){

                    val intent = Intent(this, SearchResultAty::class.java)
                    intent.putExtra("so",tag.text)
                    startActivity(intent)
                }else{
                    val parameter = KeplerAttachParameter()
                    parameter.setCustomerInfo("hlhjjz")
                    KeplerApiManager.getWebViewService().openSearchWebViewPage(tag.text, parameter)
                }
            }
        }
    }
}
